#include "stdafx.h"
#include ".\viewinfo.h"

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

viewInfo::viewInfo(void)
{
	this->m_viewOffset.x() = -100.0;
}

viewInfo::~viewInfo(void)
{
}

const mdsVector<3, double> & viewInfo::GetAnchor() const
{ 
	return m_viewCenter; 
}

mdsVector<3, double> viewInfo::GetPosition() const
{ 
	return m_viewCenter + m_viewFrame.preMultVec(m_viewOffset); 
}

mdsVector<3, double> viewInfo::GetLookDir() const { return m_viewFrame.GetRow(0); }
mdsVector<3, double> viewInfo::GetLeftDir() const { return m_viewFrame.GetRow(1); }
mdsVector<3, double> viewInfo::GetUpDir() const { return m_viewFrame.GetRow(2); }

void viewInfo::RotateBy(mdsMatrix<3, double> rotMatPreMultConvention) // uses pre-multiply convention if rotating in global frame
{
	// m_viewFrame = 
	m_viewFrame = m_viewFrame.multMat(rotMatPreMultConvention);
	// this->m_viewOffset = rotMatPreMultConvention.
}

void viewInfo::RotateByLcl(mdsMatrix<3, double> rotMatPostMultConvention) // uses pre-multiply convention if rotating in local frame
{
	m_viewFrame = rotMatPostMultConvention.multMat(m_viewFrame);
	// m_viewFrame = 
}

double viewInfo::GetViewBack() const
{
	return m_viewOffset[0];
}

void viewInfo::SetViewBack(double lfViewDist)
{
	m_viewOffset[0] = fabs(lfViewDist);
}

void viewInfo::MoveViewBack(double lfViewDist)
{
	m_viewOffset[0] += lfViewDist;
}

void viewInfo::SetAnchor(mdsVector<3, double> vecNewAnchor)
{
	m_viewCenter = vecNewAnchor;
}

void viewInfo::TranslateAnchorBy(mdsVector<3, double> vecAnchorTranslation)
{
	m_viewCenter += vecAnchorTranslation;
}

void viewInfo::SetGlViewTo() const
{
	// this->GetAnchor()
	// in OpenGL, up is +Y and look is -Z (left is +X)
	// in my conventions, up is +Z and look is +X (left is +Y)
	mdsVector<3, double> eyePos = GetPosition();
	mdsVector<3, double> upDir = GetUpDir();
	// glTranslated(-viewPos.x(), -viewPos.y(), -viewPos.z());
	
	gluLookAt(GetPosition()[0], GetPosition()[1], GetPosition()[2],
		GetAnchor()[0], GetAnchor()[1], GetAnchor()[2],
		GetUpDir()[0], GetUpDir()[1], GetUpDir()[2]);

	// glTranslated(-viewPos.y(), -viewPos.z(), viewPos.x());
	// mdsVector<3, double> 
	// glTranslated(viewPos.x(), viewPos.y(), viewPos.z());
	// glTranslated(0.0, 0.0, m_viewOffset[0]);

}
